package com.gym.code.config.admin.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Collections;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.stereotype.Repository;

import com.gym.code.config.admin.bean.method.MethodBaseMappingBean;
import com.gym.code.config.admin.bean.method.MethodBaseMappingBean.MethodParameterCombineType;
import com.gym.code.config.admin.dao.IMethodMappingDao;
import com.gym.code.generator.EnumContants.MethodOperateType;

/**
 * 
 * @author xinchun.wang 
   @email: 532002108@qq.com
 * @createTime 2015-4-11 下午5:14:43
 */
@Repository
public class MethodMappingDaoImpl implements IMethodMappingDao {
	private static final String insertSql = "INSERT INTO function_mapping_info (table_id,function_name,function_operate_type,function_parameter_type,function_result_type) VALUES(?,?,?,?,?)";
	private static final String selectSql = "SELECT id,table_id,function_name,function_operate_type,function_parameter_type,function_result_type FROM function_mapping_info ";
	
	private static final String updateSql = "update function_mapping_info set table_id=?,function_name = ?,function_operate_type =?,function_parameter_type=?,function_result_type = ?	where id = ?";

	@Autowired
	@Qualifier("jdbcTemplate")
	private JdbcTemplate jdbcTemplate;
	
	@Override
	public int insert(MethodBaseMappingBean bean) {
		final Object[] args = new Object[] {bean.getTableId(),bean.getFunctionName(),bean.getMethodOperateType().getId(),bean.getMethodParameterType().getId(),bean.getMethodResultType().getId()};
		GeneratedKeyHolder keyholder = new GeneratedKeyHolder();
		int result = jdbcTemplate.update(new PreparedStatementCreator(){
			@Override
			public PreparedStatement createPreparedStatement(Connection con)
					throws SQLException {
				PreparedStatement statement = con.prepareStatement(insertSql,Statement.RETURN_GENERATED_KEYS);
				for(int i =0;i<args.length;i++){
					statement.setObject(i+1, args[i]);
				}
				return statement;
			}
		}, keyholder);
		
		Number value = keyholder.getKey();
		if(value != null){
			bean.setId(value.longValue());
		}
		
		return result;
	}

	@Override
	public MethodBaseMappingBean queryBase(long id) {
		List<MethodBaseMappingBean> result = jdbcTemplate.query(selectSql + " where id = ?", new RowMapper<MethodBaseMappingBean>() {
			@Override
			public MethodBaseMappingBean mapRow(ResultSet rs, int rowNum)
					throws SQLException {
				MethodBaseMappingBean bean = new MethodBaseMappingBean();
				bean.setId(rs.getLong("id"));
				bean.setTableId(rs.getLong("table_id"));
				bean.setFunctionName(rs.getString("function_name"));
				bean.setMethodOperateType(MethodOperateType.getById(rs.getInt("function_operate_type")));
				bean.setMethodParameterType(MethodParameterCombineType.getById(rs.getInt("function_parameter_type")));
				bean.setMethodResultType(rs.getInt("function_result_type"));
				return bean;
			
			}
		},id);
		if(CollectionUtils.isEmpty(result)){
			return null;
		}
		return result.get(0);
	}

	@Override
	public int update(MethodBaseMappingBean bean) {
		Object[] args = new Object[] {bean.getTableId(),bean.getFunctionName(),bean.getMethodOperateType().getId(),bean.getMethodParameterType().getId(),bean.getMethodResultType().getId(),bean.getId()};
		return jdbcTemplate.update(updateSql, args);
	}

	@Override
	public List<MethodBaseMappingBean> queryBaseByTableId(long tableId) {
		List<MethodBaseMappingBean> result = jdbcTemplate.query(selectSql + " where table_id = ?", new RowMapper<MethodBaseMappingBean>() {
			@Override
			public MethodBaseMappingBean mapRow(ResultSet rs, int rowNum)
					throws SQLException {
				MethodBaseMappingBean bean = new MethodBaseMappingBean();
				bean.setId(rs.getLong("id"));
				bean.setTableId(rs.getLong("table_id"));
				bean.setFunctionName(rs.getString("function_name"));
				bean.setMethodOperateType(MethodOperateType.getById(rs.getInt("function_operate_type")));
				bean.setMethodParameterType(MethodParameterCombineType.getById(rs.getInt("function_parameter_type")));
				bean.setMethodResultType(rs.getInt("function_result_type"));
				return bean;
			
			}
		},tableId);
		if(CollectionUtils.isEmpty(result)){
			return Collections.emptyList();
		}
		return result;
	}

}
